package com.qboot.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.date.DateUtil;
import com.qboot.common.annotation.Log;
import com.qboot.common.consts.MsgConst;
import com.qboot.common.controller.BaseController;
import com.qboot.common.entity.Result;
import com.qboot.common.entity.TableResult;
import com.qboot.common.utils.EasyPoiExcelUtils;
import com.qboot.system.entity.SysPermission;
import com.qboot.system.service.SysPermissionService;
import com.qboot.system.vo.SysPermissionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 权限
 *
 * @author Tellsea
 * @date 2022-12-17
 */
@Api(tags = "权限")
@RestController
@RequestMapping("/system/sysPermission")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class SysPermissionController extends BaseController {

    private final SysPermissionService sysPermissionService;

    @ApiOperation("权限查询")
    @SaCheckPermission("system:sysPermission:list")
    @GetMapping("/list")
    public TableResult<SysPermissionVo> queryList(SysPermissionVo entity) {
        return TableResult.ok(sysPermissionService.queryList(getAllPage(), entity));
    }

    @ApiOperation("权限导入")
    @Log("权限导入")
    @SaCheckPermission("system:sysPermission:import")
    @PostMapping("/importExcel")
    public Result importExcel(@RequestParam("file") MultipartFile file) {
        List<SysPermission> list = EasyPoiExcelUtils.importExcel(file, 1, 1, SysPermission.class);
        sysPermissionService.saveBatch(list, list.size());
        return Result.ok(MsgConst.IMPORT_SUCCESS + list.size() + "条数据");
    }

    @ApiOperation("权限导出")
    @Log("权限导出")
    @SaCheckPermission("system:sysPermission:export")
    @GetMapping("/exportExcel")
    public void exportExcel(SysPermissionVo entity) {
        List<SysPermissionVo> list = sysPermissionService.queryList(getAllPage(), entity).getRecords();
        EasyPoiExcelUtils.exportExcel(list, "权限-" + DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss"), SysPermissionVo.class);
    }

    @ApiOperation("权限根据主键查询")
    @Log("权限根据主键查询")
    @SaCheckPermission("system:sysPermission:get")
    @GetMapping("/queryById/{id}")
    public Result<SysPermissionVo> queryById(@PathVariable("id") Long id) {
        return queryResult(sysPermissionService.queryById(id));
    }

    @ApiOperation("权限新增")
    @Log("权限新增")
    @SaCheckPermission("system:sysPermission:save")
    @PostMapping("/save")
    public Result save(@RequestBody SysPermissionVo entity) {
        return saveResult(sysPermissionService.save(entity));
    }

    @ApiOperation("权限根据主键更新")
    @Log("权限根据主键更新")
    @SaCheckPermission("system:sysPermission:update")
    @PostMapping("/updateById")
    public Result updateById(@RequestBody SysPermissionVo entity) {
        return updateResult(sysPermissionService.updateById(entity));
    }

    @ApiOperation("权限根据主键删除")
    @Log("权限根据主键删除")
    @SaCheckPermission("system:sysPermission:remove")
    @GetMapping("/removeById/{id}")
    public Result removeById(@PathVariable("id") Long id) {
        return removeResult(sysPermissionService.removeById(id));
    }

    @ApiOperation("权限根据主键批量删除")
    @Log("权限根据主键批量删除")
    @SaCheckPermission("system:sysPermission:removeBatch")
    @GetMapping("/removeBatchByIds/{ids}")
    public Result removeBatchByIds(@PathVariable("ids") List<Long> ids) {
        return removeResult(sysPermissionService.removeBatchByIds(ids));
    }
}
